#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
#include "../wxsitemresdata.h"
#include "../wxsitem.h"
#include "wxsimagelisteditordlg.h"
#include "wxsimagecomboeditordlg.h"

const long wxsImageComboEditorDlg::ID_STATICTEXT1 = wxNewId();
const long wxsImageComboEditorDlg::ID_TREECTRL1 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT2 = wxNewId();
const long wxsImageComboEditorDlg::ID_BITMAPBUTTON1 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT3 = wxNewId();
const long wxsImageComboEditorDlg::ID_BITMAPBUTTON2 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT4 = wxNewId();
const long wxsImageComboEditorDlg::ID_BITMAPBUTTON3 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT5 = wxNewId();
const long wxsImageComboEditorDlg::ID_BITMAPBUTTON4 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT6 = wxNewId();
const long wxsImageComboEditorDlg::ID_BITMAPBUTTON5 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT7 = wxNewId();
const long wxsImageComboEditorDlg::ID_BITMAPBUTTON6 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT8 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT9 = wxNewId();
const long wxsImageComboEditorDlg::ID_COMBOBOX1 = wxNewId();
const long wxsImageComboEditorDlg::ID_STATICTEXT10 = wxNewId();
const long wxsImageComboEditorDlg::ID_BUTTON1 = wxNewId();
const long wxsImageComboEditorDlg::ID_BUTTON2 = wxNewId();

BEGIN_EVENT_TABLE( wxsImageComboEditorDlg, wxScrollingDialog )


END_EVENT_TABLE()

wxsImageComboEditorDlg::wxsImageComboEditorDlg( wxWindow* parent ) {
  wxBoxSizer* BoxSizer4;
  wxBoxSizer* BoxSizer6;
  wxBoxSizer* BoxSizer5;
  wxBoxSizer* BoxSizer10;
  wxBoxSizer* BoxSizer7;
  wxBoxSizer* BoxSizer8;
  wxBoxSizer* BoxSizer13;
  wxBoxSizer* BoxSizer2;
  wxBoxSizer* BoxSizer11;
  wxBoxSizer* BoxSizer12;
  wxBoxSizer* BoxSizer14;
  wxBoxSizer* BoxSizer1;
  wxBoxSizer* BoxSizer9;
  wxBoxSizer* BoxSizer3;
  Create( parent, wxID_ANY, _T( "wxBitmapComboBox Editor" ), wxDefaultPosition, wxDefaultSize, wxCAPTION | wxDEFAULT_DIALOG_STYLE, _T( "wxID_ANY" ) );
  SetMinSize( wxSize( -1, -1 ) );
  SetMaxSize( wxSize( -1, -1 ) );
  BoxSizer1 = new wxBoxSizer( wxVERTICAL );
  BoxSizer13 = new wxBoxSizer( wxHORIZONTAL );
  BoxSizer2 = new wxBoxSizer( wxVERTICAL );
  StaticText1 = new wxStaticText( this, ID_STATICTEXT1, _T( "Combo-Box" ), wxDefaultPosition, wxDefaultSize, wxST_NO_AUTORESIZE | wxALIGN_CENTRE, _T( "ID_STATICTEXT1" ) );
  BoxSizer2->Add( StaticText1, 0, wxALL | wxALIGN_LEFT, 5 );
  m_pTree = new wxTreeCtrl( this, ID_TREECTRL1, wxPoint( 0, 32 ), wxSize( 240, 272 ), wxTR_EDIT_LABELS | wxTR_NO_BUTTONS | wxTR_NO_LINES | wxTR_HIDE_ROOT | wxRAISED_BORDER, wxDefaultValidator, _T( "ID_TREECTRL1" ) );
  BoxSizer2->Add( m_pTree, 0, wxALL | wxEXPAND, 0 );
  BoxSizer13->Add( BoxSizer2, 0, wxALL | wxALIGN_TOP, 5 );
  BoxSizer3 = new wxBoxSizer( wxVERTICAL );
  StaticText2 = new wxStaticText( this, ID_STATICTEXT2, _T( "编辑项目" ), wxPoint( 264, 8 ), wxDefaultSize, wxST_NO_AUTORESIZE | wxALIGN_CENTRE, _T( "ID_STATICTEXT2" ) );
  BoxSizer3->Add( StaticText2, 0, wxALL | wxALIGN_LEFT, 5 );
  BoxSizer4 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnAddItem = new wxBitmapButton( this, ID_BITMAPBUTTON1, wxArtProvider::GetBitmap( wxART_MAKE_ART_ID_FROM_STR( _T( "wxART_ADD_BOOKMARK" ) ), wxART_BUTTON ), wxDefaultPosition, wxSize( 32, 32 ), wxBU_AUTODRAW, wxDefaultValidator, _T( "ID_BITMAPBUTTON1" ) );
  m_pBtnAddItem->SetDefault();
  BoxSizer4->Add( m_pBtnAddItem, 0, wxALL | wxALIGN_CENTER_VERTICAL, 0 );
  StaticText3 = new wxStaticText( this, ID_STATICTEXT3, _T( "添加项目" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT3" ) );
  BoxSizer4->Add( StaticText3, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer3->Add( BoxSizer4, 0, wxALL | wxEXPAND, 0 );
  BoxSizer5 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnMoveUp = new wxBitmapButton( this, ID_BITMAPBUTTON2, wxArtProvider::GetBitmap( wxART_MAKE_ART_ID_FROM_STR( _T( "wxART_GO_UP" ) ), wxART_BUTTON ), wxDefaultPosition, wxSize( 32, 32 ), wxBU_AUTODRAW, wxDefaultValidator, _T( "ID_BITMAPBUTTON2" ) );
  m_pBtnMoveUp->SetDefault();
  BoxSizer5->Add( m_pBtnMoveUp, 0, wxALL | wxALIGN_CENTER_VERTICAL, 0 );
  StaticText4 = new wxStaticText( this, ID_STATICTEXT4, _T( "Move Item Up" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT4" ) );
  BoxSizer5->Add( StaticText4, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer3->Add( BoxSizer5, 0, wxALL | wxEXPAND, 0 );
  BoxSizer6 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnMoveDown = new wxBitmapButton( this, ID_BITMAPBUTTON3, wxArtProvider::GetBitmap( wxART_MAKE_ART_ID_FROM_STR( _T( "wxART_GO_DOWN" ) ), wxART_BUTTON ), wxDefaultPosition, wxSize( 32, 32 ), wxBU_AUTODRAW, wxDefaultValidator, _T( "ID_BITMAPBUTTON3" ) );
  m_pBtnMoveDown->SetDefault();
  BoxSizer6->Add( m_pBtnMoveDown, 0, wxALL | wxALIGN_CENTER_VERTICAL, 0 );
  StaticText5 = new wxStaticText( this, ID_STATICTEXT5, _T( "Move Item Down" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT5" ) );
  BoxSizer6->Add( StaticText5, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer3->Add( BoxSizer6, 0, wxALL | wxEXPAND, 0 );
  BoxSizer7 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnDeleteItem = new wxBitmapButton( this, ID_BITMAPBUTTON4, wxArtProvider::GetBitmap( wxART_MAKE_ART_ID_FROM_STR( _T( "wxART_DEL_BOOKMARK" ) ), wxART_BUTTON ), wxDefaultPosition, wxSize( 32, 32 ), wxBU_AUTODRAW, wxDefaultValidator, _T( "ID_BITMAPBUTTON4" ) );
  BoxSizer7->Add( m_pBtnDeleteItem, 0, wxALL | wxALIGN_CENTER_VERTICAL, 0 );
  StaticText6 = new wxStaticText( this, ID_STATICTEXT6, _T( "删除项目" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT6" ) );
  BoxSizer7->Add( StaticText6, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer3->Add( BoxSizer7, 0, wxALL | wxEXPAND, 0 );
  BoxSizer8 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnDeleteAll = new wxBitmapButton( this, ID_BITMAPBUTTON5, wxArtProvider::GetBitmap( wxART_MAKE_ART_ID_FROM_STR( _T( "wxART_DELETE" ) ), wxART_BUTTON ), wxDefaultPosition, wxSize( 32, 32 ), wxBU_AUTODRAW, wxDefaultValidator, _T( "ID_BITMAPBUTTON5" ) );
  BoxSizer8->Add( m_pBtnDeleteAll, 0, wxALL | wxALIGN_CENTER_VERTICAL, 0 );
  StaticText7 = new wxStaticText( this, ID_STATICTEXT7, _T( "清空项目" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT7" ) );
  BoxSizer8->Add( StaticText7, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer3->Add( BoxSizer8, 0, wxALL | wxEXPAND, 0 );
  BoxSizer9 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnEditItem = new wxBitmapButton( this, ID_BITMAPBUTTON6, wxArtProvider::GetBitmap( wxART_MAKE_ART_ID_FROM_STR( _T( "wxART_HELP_SETTINGS" ) ), wxART_BUTTON ), wxDefaultPosition, wxSize( 32, 32 ), wxBU_AUTODRAW, wxDefaultValidator, _T( "ID_BITMAPBUTTON6" ) );
  m_pBtnEditItem->SetDefault();
  BoxSizer9->Add( m_pBtnEditItem, 0, wxALL | wxALIGN_CENTER_VERTICAL, 0 );
  StaticText8 = new wxStaticText( this, ID_STATICTEXT8, _T( "Edit Item Text" ), wxDefaultPosition, wxDefaultSize, 0, _T( "ID_STATICTEXT8" ) );
  BoxSizer9->Add( StaticText8, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer3->Add( BoxSizer9, 0, wxALL | wxEXPAND, 0 );
  BoxSizer10 = new wxBoxSizer( wxVERTICAL );
  BoxSizer11 = new wxBoxSizer( wxHORIZONTAL );
  StaticText9 = new wxStaticText( this, ID_STATICTEXT9, _T( "Image-List" ), wxPoint( 256, 240 ), wxDefaultSize, wxST_NO_AUTORESIZE, _T( "ID_STATICTEXT9" ) );
  BoxSizer11->Add( StaticText9, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer10->Add( BoxSizer11, 0, wxALL | wxALIGN_LEFT, 0 );
  BoxSizer12 = new wxBoxSizer( wxHORIZONTAL );
  m_pCmbImage = new wxBitmapComboBox( this, ID_COMBOBOX1, wxEmptyString, wxPoint( 256, 272 ), wxSize( 100, 22 ), 0, 0, wxCB_READONLY, wxDefaultValidator, _T( "ID_COMBOBOX1" ) );
  BoxSizer12->Add( m_pCmbImage, 0, wxTOP | wxBOTTOM | wxRIGHT | wxALIGN_CENTER_VERTICAL, 5 );
  StaticText10 = new wxStaticText( this, ID_STATICTEXT10, _T( "Item Image" ), wxPoint( 352, 276 ), wxDefaultSize, 0, _T( "ID_STATICTEXT10" ) );
  BoxSizer12->Add( StaticText10, 0, wxALL | wxALIGN_CENTER_VERTICAL, 5 );
  BoxSizer10->Add( BoxSizer12, 0, wxALL | wxALIGN_LEFT, 0 );
  BoxSizer3->Add( BoxSizer10, 1, wxTOP | wxBOTTOM | wxRIGHT | wxEXPAND, 5 );
  BoxSizer13->Add( BoxSizer3, 0, wxALL | wxALIGN_TOP, 5 );
  BoxSizer1->Add( BoxSizer13, 0, wxALL | wxALIGN_LEFT, 5 );
  BoxSizer14 = new wxBoxSizer( wxHORIZONTAL );
  m_pBtnOK = new wxButton( this, ID_BUTTON1, _T( "OK" ), wxPoint( 64, 320 ), wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON1" ) );
  m_pBtnOK->SetDefault();
  BoxSizer14->Add( m_pBtnOK, 0, wxALL | wxALIGN_BOTTOM, 5 );
  BoxSizer14->Add( -1, -1, 1, wxALL | wxALIGN_BOTTOM, 5 );
  m_pBtnCancel = new wxButton( this, ID_BUTTON2, _T( "Cancel" ), wxPoint( 272, 320 ), wxDefaultSize, 0, wxDefaultValidator, _T( "ID_BUTTON2" ) );
  BoxSizer14->Add( m_pBtnCancel, 0, wxALL | wxALIGN_BOTTOM, 5 );
  BoxSizer1->Add( BoxSizer14, 0, wxALL | wxEXPAND, 5 );
  SetSizer( BoxSizer1 );
  BoxSizer1->Fit( this );
  BoxSizer1->SetSizeHints( this );
  Connect( ID_TREECTRL1, wxEVT_COMMAND_TREE_SEL_CHANGED, wxTreeEventHandler( wxsImageComboEditorDlg::OnTreeSelectionChanged ) );
  Connect( ID_BITMAPBUTTON1, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnAddItemClick ) );
  Connect( ID_BITMAPBUTTON2, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnMoveUpClick ) );
  Connect( ID_BITMAPBUTTON3, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnMoveDownClick ) );
  Connect( ID_BITMAPBUTTON4, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnDeleteItemClick ) );
  Connect( ID_BITMAPBUTTON5, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnDeleteAllClick ) );
  Connect( ID_BITMAPBUTTON6, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnEditItemClick ) );
  Connect( ID_COMBOBOX1, wxEVT_COMMAND_COMBOBOX_SELECTED, wxCommandEventHandler( wxsImageComboEditorDlg::OnCmbImageSelect ) );
  Connect( ID_BUTTON1, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnOKClick ) );
  Connect( ID_BUTTON2, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( wxsImageComboEditorDlg::OnBtnCancelClick ) );
}

wxsImageComboEditorDlg::~wxsImageComboEditorDlg() {
}


bool wxsImageComboEditorDlg::Execute( wxArrayString &aItems ) {
  int             i, n;
  int             j, k;
  long            ll;
  wxString        ss, tt;
  wxTreeItemId    root;
  wxTreeItemId    item;
  wxTreeItemIdValue   cookie;
  wxBitmap        bmp;
  wxsImageList    *ilist;
  n = aItems.GetCount();
  if( n == 0 ) {
    m_ComboName = _T( "<unknown>" );
    m_ImageName = _T( "<none>" );
  } else if( n == 1 ) {
    m_ComboName = aItems.Item( 0 );
    m_ImageName = _T( "<none>" );
  } else {
    m_ComboName = aItems.Item( 0 );
    m_ImageName = aItems.Item( 1 );
  }
  ss = _T( "Combo Box: " ) + m_ComboName;
  StaticText1->SetLabel( ss );
  ss = _T( "Image List: " ) + m_ImageName;
  StaticText9->SetLabel( ss );
  m_ImageList.RemoveAll();
  ilist = ( wxsImageList * ) wxsImageListEditorDlg::FindTool( NULL, m_ImageName );
  if( ilist == NULL ) {
    m_pCmbImage->Enable( false );
  } else {
    m_pCmbImage->Enable( true );
    ilist->GetImageList( m_ImageList );
  }
  m_pCmbImage->Clear();
  m_pCmbImage->Append( _T( "<none>" ) );
  n = m_ImageList.GetImageCount();
  for( i = 0; i < n; i++ ) {
    ss.Printf( _T( "%3d" ), i );
    bmp = m_ImageList.GetBitmap( i );
    m_pCmbImage->Append( ss, bmp );
  }
  m_pCmbImage->SetSelection( 0 );
  m_pTree->DeleteAllItems();
  root = m_pTree->AddRoot( _T( "root" ) );
  m_pTree->SetImageList( &m_ImageList );
  n = aItems.GetCount();
  for( i = 2; i < n; i++ ) {
    ss = aItems.Item( i );
    j  = ss.Find( _T( "," ) );
    k = -1;
    if( j != wxNOT_FOUND ) {
      tt = ss.Left( j );
      ss.erase( 0, j + 1 );
      if( tt.ToLong( &ll ) ) {
        k = ll;
      }
    }
    item = m_pTree->AppendItem( root, ss, k );
  }
  n = ShowModal();
  if( n == wxOK ) {
    aItems.Clear();
    aItems.Add( m_ComboName );
    aItems.Add( m_ImageName );
    root = m_pTree->GetRootItem();
    if( root.IsOk() ) {
      item = m_pTree->GetFirstChild( root, cookie );
      while( item.IsOk() ) {
        ss = m_pTree->GetItemText( item );
        k  = m_pTree->GetItemImage( item, wxTreeItemIcon_Normal );
        tt.Printf( _T( "%d," ), k );
        ss = tt + ss;
        aItems.Add( ss );
        item = m_pTree->GetNextChild( root, cookie );
      }
    }
  }
  return ( n == wxOK );
}


void wxsImageComboEditorDlg::OnBtnDeleteAllClick( M_Code_unused wxCommandEvent& event ) {
  int n;
  n = wxMessageBox( _T( "清空项目?" ), _T( "Confirm" ), wxYES_NO );
  if( n == wxYES ) {
    m_pTree->DeleteAllItems();
  }
}

void wxsImageComboEditorDlg::OnBtnAddItemClick( M_Code_unused wxCommandEvent& event ) {
  int n;
  wxTreeItemId root = m_pTree->GetRootItem();
  wxTreeItemId item;
  wxString ss;
  n = m_pTree->GetCount();
  if( !root.IsOk() ) {
    root = m_pTree->AddRoot( _T( "root" ) );
  }
  ss.Printf( _T( "new item %d" ), ++n );
  item = m_pTree->AppendItem( root, ss );
  n = m_pCmbImage->GetSelection();
  if( n == wxNOT_FOUND ) {
    n  = -1;
  } else {
    n -=  1;
  }
  m_pTree->SetItemImage( item, n, wxTreeItemIcon_Normal );
  m_pTree->SelectItem( item );
  m_pTree->EditLabel( item );
}


void wxsImageComboEditorDlg::OnBtnMoveUpClick( M_Code_unused wxCommandEvent& event ) {
  int xi, xp;
  wxString si, sp;
  wxTreeItemId prev;
  wxTreeItemId item;
  item = m_pTree->GetSelection();
  prev = m_pTree->GetPrevSibling( item );
  if( ! item.IsOk() ) {
    return;
  }
  if( ! prev.IsOk() ) {
    return;
  }
  si = m_pTree->GetItemText( item );
  xi = m_pTree->GetItemImage( item, wxTreeItemIcon_Normal );
  sp = m_pTree->GetItemText( prev );
  xp = m_pTree->GetItemImage( prev, wxTreeItemIcon_Normal );
  m_pTree->SetItemText( item, sp );
  m_pTree->SetItemImage( item, xp );
  m_pTree->SetItemText( prev, si );
  m_pTree->SetItemImage( prev, xi );
  m_pTree->SelectItem( prev );
  m_pTree->Refresh();
}


void wxsImageComboEditorDlg::OnBtnMoveDownClick( M_Code_unused wxCommandEvent& event ) {
  int xi, xp;
  wxString si, sp;
  wxTreeItemId next;
  wxTreeItemId item;
  item = m_pTree->GetSelection();
  next = m_pTree->GetNextSibling( item );
  if( !item.IsOk() ) {
    return;
  }
  if( !next.IsOk() ) {
    return;
  }
  si = m_pTree->GetItemText( item );
  xi = m_pTree->GetItemImage( item, wxTreeItemIcon_Normal );
  sp = m_pTree->GetItemText( next );
  xp = m_pTree->GetItemImage( next, wxTreeItemIcon_Normal );
  m_pTree->SetItemText( item, sp );
  m_pTree->SetItemImage( item, xp );
  m_pTree->SetItemText( next, si );
  m_pTree->SetItemImage( next, xi );
  m_pTree->SelectItem( next );
  m_pTree->Refresh();
}


void wxsImageComboEditorDlg::OnBtnDeleteItemClick( M_Code_unused wxCommandEvent& event ) {
  wxTreeItemId    item;
  item = m_pTree->GetSelection();
  if( item.IsOk() ) {
    m_pTree->Delete( item );
  }
  m_pTree->Refresh();
}

void wxsImageComboEditorDlg::OnBtnEditItemClick( M_Code_unused wxCommandEvent& event ) {
  wxTreeItemId    item;
  item = m_pTree->GetSelection();
  if( item.IsOk() ) {
    m_pTree->EditLabel( item );
  }
  m_pTree->Refresh();
}


void wxsImageComboEditorDlg::OnBtnOKClick( M_Code_unused wxCommandEvent& event ) {
  EndModal( wxOK );
}


void wxsImageComboEditorDlg::OnBtnCancelClick( M_Code_unused wxCommandEvent& event ) {
  EndModal( wxCANCEL );
}


void wxsImageComboEditorDlg::OnCmbImageSelect( M_Code_unused wxCommandEvent& event ) {
  int n;
  wxTreeItemId item;
  item = m_pTree->GetSelection();
  if( ! item.IsOk() ) {
    return;
  }
  n = m_pCmbImage->GetSelection();
  if( n == wxNOT_FOUND ) {
    n  = -1;
  } else {
    n -=  1;
  }
  m_pTree->SetItemImage( item, n, wxTreeItemIcon_Normal );
}


void wxsImageComboEditorDlg::OnTreeSelectionChanged( wxTreeEvent& event ) {
  wxTreeItemId item = event.GetItem();
  if( item.IsOk() ) {
    m_pCmbImage->SetSelection( m_pTree->GetItemImage( item ) + 1 );
  }
}
